import { KJUR } from 'jsrsasign';

export const setProperty = (prop: string, val: any, dom = document.documentElement) => {
    dom.style.setProperty(prop, val);
}

export const mix = (color1: string, color2: string, weight: number = 0.5): string => {
    let color = '#';
    for (let i = 0; i <= 2; i++) {
        const c1 = parseInt(color1.substring(1 + i * 2, i * 2 + 3), 16);
        const c2 = parseInt(color2.substring(1 + i * 2, i * 2 + 3), 16);
        const c = Math.floor(c1 * weight + c2 * (1 - weight));
        color += c.toString(16).padStart(2, '0');
    }
    return color;
}


export function generateToken(userId: string | number = 666, secret: string = 'wzzz', expiration: number = 86400) {
    const header = { alg: 'HS512', typ: 'JWT' };
    const now = Math.floor(Date.now() / 1000);
    const payload = {
        sub: userId.toString(),
        iat: now,
        exp: expiration
    };

    const sHeader = JSON.stringify(header);
    const sPayload = JSON.stringify(payload);

    const token = KJUR.jws.JWS.sign("HS512", sHeader, sPayload, secret);
    return token;
}
